﻿Imports ControladorasAT.SIREOS
Imports ServiciosAT
Imports Utilitarios
Imports System.Data
Imports System.Drawing

Partial Class UserControl_MantenimientoCliente
    Inherits System.Web.UI.UserControl

    Dim selID As Decimal

    Private Sub buscarCliente()
        Dim oSedeBER As New SireosWSCliente.Sede

        Dim oClienteBER As New SireosWSCliente.Cliente
        oClienteBER.nomCliente = txbCliente.Text
        oClienteBER.nroDocumento = txbDocumento.Text

        oSedeBER.cliente = oClienteBER
        oSedeBER.nomSede = txbSede.Text
        oSedeBER.telSede = txbTelefono.Text

        Dim oCtCliente As New CtCliente
        Dim dt As DataTable = oCtCliente.buscarCliente(oSedeBER)
        ocultarColumnas(True)
        gridClientes.DataSource = dt
        gridClientes.DataBind()
        ocultarColumnas(False)
        If dt Is Nothing Then
            LblTotal.Text = "Total: 0 Registros"
        Else
            LblTotal.Text = "Total: " + dt.Rows.Count.ToString + " Registros"
        End If
        Session("SelCli") = Valores.vacio
    End Sub

    Private Sub ocultarColumnas(est As Boolean)
        gridClientes.Columns(0).Visible = est
        gridClientes.Columns(1).Visible = est
        gridClientes.Columns(2).Visible = est
    End Sub

    Protected Sub imgBuscar_Click(sender As Object, e As ImageClickEventArgs) Handles imgBuscar.Click
        buscarCliente()
    End Sub

    Protected Sub gridClientes_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles gridClientes.PageIndexChanging
        buscarCliente()
        ocultarColumnas(True)
        gridClientes.PageIndex = e.NewPageIndex
        gridClientes.DataBind()
        ocultarColumnas(False)
    End Sub

    Protected Sub gridClientes_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gridClientes.RowDataBound
        Dim oImageButton As ImageButton = Nothing
        Try
            If e.Row.RowType = DataControlRowType.DataRow Then
                oImageButton = DirectCast(e.Row.FindControl("ibtnSeleccionar"), ImageButton)
                If oImageButton IsNot Nothing Then
                    oImageButton.CommandArgument = Convert.ToString(e.Row.RowIndex)
                End If
                oImageButton = DirectCast(e.Row.FindControl("ibtnEditar"), ImageButton)
                If oImageButton IsNot Nothing Then
                    oImageButton.CommandArgument = Convert.ToString(e.Row.RowIndex)
                End If
                oImageButton = DirectCast(e.Row.FindControl("ibtnAnularSede"), ImageButton)
                If oImageButton IsNot Nothing Then
                    oImageButton.CommandArgument = Convert.ToString(e.Row.RowIndex)
                End If

            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Protected Sub gridClientes_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles gridClientes.RowCommand
        Try
            selID = Convert.ToInt32(gridClientes.Rows(Convert.ToInt32(e.CommandArgument)).Cells(0).Text)
            If e.CommandName = "cnSeleccionar" Then
                gridClientes.Rows(Convert.ToInt32(e.CommandArgument)).BackColor = Color.LightSalmon
                If Session("SelCli") <> Valores.vacio Then
                    gridClientes.Rows(CInt(Session("SelCli"))).BackColor = Color.White
                End If
                Session("SelCli") = Convert.ToInt32(e.CommandArgument).ToString
                Session("CodigoTipoCliente") = Convert.ToInt32(gridClientes.Rows(Convert.ToInt32(e.CommandArgument)).Cells(2).Text)
                Session("CodigoCliente") = Convert.ToInt32(gridClientes.Rows(Convert.ToInt32(e.CommandArgument)).Cells(1).Text)
                Session("NroDocumento") = gridClientes.Rows(Convert.ToInt32(e.CommandArgument)).Cells(3).Text
                Session("CodigoSede") = selID.ToString
            ElseIf e.CommandName = "cnEditar" Then
                Session("CodigoSede") = selID.ToString
                Session("CodigoCliente") = Convert.ToInt32(gridClientes.Rows(Convert.ToInt32(e.CommandArgument)).Cells(1).Text)
                ClienteRegistro2.cargarDatosCliente()
                panelFondo.Visible = True
                panelCliente.Visible = True
            ElseIf e.CommandName = "cnAnular" Then
                Dim oSede As New SireosWSCliente.Sede
                oSede.codSede = selID.ToString()
                Dim oCtCliente As New CtCliente
                oCtCliente.anularSede(oSede)
                Helper.mensajeControl(Me, Mensajes.correctoAnulado)
                buscarCliente()
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Protected Sub imgAgregar_Click(sender As Object, e As ImageClickEventArgs) Handles imgAgregar.Click
        ClienteRegistro2.limpiarCampos()
        panelFondo.Visible = True
        panelCliente.Visible = True
    End Sub

    Protected Sub ibtnCerrarRegistroCliente_Click(sender As Object, e As ImageClickEventArgs) Handles ibtnCerrarRegistroCliente.Click
        panelFondo.Visible = False
        panelCliente.Visible = False
    End Sub

End Class
